<?php
/**
 * 用户红包
 * Created by PhpStorm.
 * User: poptser
 * Email: poptser@foxmail.com
 * Date: 2016/8/15
 * Time: 11:27
 */

namespace YMApi\Model;


use Think\Model;

class UserbonusModel extends Model
{
    protected $tableName = 'user_bonus';

    /**
     * 获取红包列表
     * @param null $uid
     * @param int $current_page
     * @return bool
     */
    public function get_user_bonus($uid=null, $current_page=1){
        if(empty($uid)) return false;
        $collect_all = $this->where(array("user_id"=>$uid))->count();
        if($collect_all) {
            $return_data['sumcount'] = $collect_all;
            $all_page = ceil($collect_all / C("page_size"));
            $start = ($current_page-1) *C("page_size");
            $return_data['sumcount'] = $collect_all;
            if ($all_page == $current_page) {
                //判断是否为最后一页
                $return_data['is_bottom'] = 1;
            } else {
                $return_data['is_bottom'] = 0;
            }
//             $bonus_data = $this->where(array("user_id"=>$uid))->limit($start,C("page_size"))->order("used_time desc")->select();
//             foreach ($bonus_data as $key=>$value){

//                 $bonus_type = M("bonus_type")->where('type_id='.$value['bonus_type_id'])->find();
//                 $bonus_data[$key]['type_name'] = $bonus_type['type_name'];
//                 $bonus_data[$key]['type_money'] = $bonus_type['type_money'];
//                 $bonus_data[$key]['min_amount'] = $bonus_type['min_amount'];
//                 $bonus_data[$key]['use_start_date'] = $bonus_type['use_start_date'];
//                 $bonus_data[$key]['use_end_date'] = $bonus_type['use_end_date'];
//                 $bonus_data[$key]['min_goods_amount'] = $bonus_type['min_goods_amount'];
//             }
            $curtime = gmtime();
            $bonus_data = $this->alias('ub')->field("ub.bonus_id,ub.bonus_type_id,ub.bonus_sn,ub.user_id,ub.used_time,ub.order_id,ub.emailed,ub.deadline,bt.*,if(ub.used_time>0,1,0) as is_use,if(ub.deadline>0 and ub.deadline<$curtime,1,if(ub.deadline=0 and bt.use_end_date<$curtime,1,0)) as is_time_out,if(ub.deadline>0,ub.deadline,bt.use_end_date) as can_use_time")->join("left join ".C('DB_PREFIX')."bonus_type bt on ub.bonus_type_id=bt.type_id")->where(array("ub.user_id"=>$uid,"bt.bonus_type" => 0))->limit($start,C("page_size"))->order("is_use asc,is_time_out asc,can_use_time asc")->select();
            foreach ($bonus_data as $key=>$data){
                if($data['deadline']>0){
                    $bonus_data[$key]['use_end_date'] = localtotime($data['deadline']);
                }else{
                    $bonus_data[$key]['use_end_date'] = localtotime($data['use_end_date']);
                }

            	if($data['is_limit_buy']==1){
            		$bonus_config = M('bonus_config')->where(array("bonus_type_id"=>$data['bonus_type_id']))->select();
            		$bonus_config_list = array();
            		foreach ($bonus_config as $cfg_key=>$cfg_data){
            			if($cfg_data['status']==0){
            				continue;
            			}
            			switch ($cfg_data['act_rang'])	{
            				case 1:
			    				$id = $cfg_data['cate_id'];
			    				$category = M('category')->field('cat_name')->where(array('cat_id'=>$id))->find();
			    				$bonus_config[$cfg_key]['typename'] = "分类";
			    				$bonus_config[$cfg_key]['name'] = $category['cat_name'];
			    			break;
			    			case 2:
			    				$id = $cfg_data['brand_id'];
			    				$brand = M('brand')->field('brand_name')->where(array('brand_id'=>$id))->find();
			    				$bonus_config[$cfg_key]['typename'] = "品牌";
			    				$bonus_config[$cfg_key]['name'] = $brand['brand_name'];
			    			break;
			    			case 3:
			    				$id = $cfg_data['goods_id'];
			    				$goods = M('goods')->field('goods_name')->where(array('goods_id'=>$id))->find();
			    				$bonus_config[$cfg_key]['typename'] = "商品";
			    				$bonus_config[$cfg_key]['name'] = $goods['goods_name'];
		    			break;
            			}
            			$bonus_config_list[] = $bonus_config[$cfg_key];
            		}
            		$bonus_data[$key]['limit_info'] = $bonus_config_list;
            	}else{
            		$bonus_data[$key]['limit_info'] = array();
            	}
            }
            $return_data['list'] = $bonus_data;
            return $return_data;
        }else{
            return false;
        }
    }

    /**
     * 新加红包
     * @param null $uid
     * @param $bonus_sn
     * @return bool
     */
    public function new_user_bonus($uid=null, $bonus_sn){
        if(empty($uid)||empty($bonus_sn)){
            return false;
        }
        /* 查询红包序列号是否已经存在 */
        $row = $this->field("bonus_id, bonus_sn, user_id, bonus_type_id")->where(array('bonus_sn'=>$bonus_sn))->find();
        if ($row) {
            if ($row['user_id'] == 0) {
                //红包没有被使用
                $bonus_time = M('bonus_type')->field("send_end_date,use_end_date,has_effective_days,effective_days")->where(array("type_id"=>$row['bonus_type_id']))->find();

                $now = gmtime();
                if ($now > $bonus_time['use_end_date']) {
                    return false;   //红包过期
                }

                //如果设置了个人红包的使用有效期，则根据当前领取的时间计算红包的截止使用时间 add by yangzhic on 2016-12-13
                if($bonus_time['has_effective_days']==1 && $bonus_time['effective_days']>0){
                    $end_time = time() + ($bonus_time['effective_days']-1)*24*3600;
                    $end_date = date('Y-m-d',$end_time)." 23:59:59";
                    $deadline = local_strtotime($end_date); //转换成GMT格林威治时间戳
                }else{
                    $deadline = 0;
                }

                $bonus_data = array();
                $bonus_data['user_id'] = $uid;
                $bonus_data['get_time'] = $now;
                $bonus_data['deadline'] = $deadline;

                $result = $this->where(array("bonus_id"=>$row['bonus_id']))->save($bonus_data);
                if ($result!==false) {
                    return $this->get_honus_info($uid,$bonus_sn);
                } else {
                    return false;//更改失败
                }
            } else {
                if ($row['user_id'] == $uid) {
                    //红包已经添加过了。
                    return false;
                } else {
                    //红包被其他人使用过了。
                    return false;
                }
            }
        } else {
            //红包不存在
            return false;
        }
    }

    /**
     * 获取红包详细
     * @param null $uid
     * @param $bonus_sn
     * @return bool|mixed
     */
    public function get_honus_info($uid=null, $bonus_sn){
        if (empty($bonus_sn)) return false;

        $bonus_data = $this->where(array("user_id"=>$uid,"bonus_sn"=>$bonus_sn))->find();
        if ($bonus_data){
            return $bonus_data;
        }else{
            return false;
        }
    }

}